package com.scrumtrek.trainings.tdd;

import java.sql.*;
import java.sql.Connection;

/**
 * Created with IntelliJ IDEA.
 * User: neee
 * Date: 13.09.13
 * Time: 11:16
 * To change this template use File | Settings | File Templates.
 */
public class DBWorker {
    private PreparedStatement ps;
    private java.sql.Connection connection;

    public PreparedStatement getPs() {
        return ps;
    }

    public void setPs(PreparedStatement ps) {
        this.ps = ps;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public void initializeConnection() {
        if(connection == null) {
            connection = null;
            try {
                connection = getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setPSFromSql(String sql) throws SQLException {
        ps = connection.prepareStatement(sql);
    }

    public ResultSet executeQuery() throws SQLException {
        return ps.executeQuery();
    }

    public Connection getConnectionJNDI() throws Exception {
        Connection connection = null;
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost/bookstore", "root", "");
        return connection;
    }
}
